package com.xiaoyu.servlet;


import com.xiaoyu.dao.BaseDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class addPerson extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String phone = req.getParameter("phone");
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        Object[] params ={name, sex, age, phone};
        try {
            conn = BaseDao.getConnection();
            String sql = "INSERT INTO `contact` (`name`, `sex`, `age`, `phone`) VALUES (?, ?, ?, ?);";
            pst = conn.prepareStatement(sql);
            int i = BaseDao.execute(conn, sql, params, pst);
            if(i>0){
                System.out.println("插入成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.release(conn, pst, rs);
        }
        resp.sendRedirect(req.getContextPath()+"/page.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
